package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 菜单控制 数据访问层接口
 */
@Repository
public interface MenuDao {
    /**
     * 分页查询
     *
     * @param queryString 查询条件
     * @return
     */
    Page<Menu> findPage(String queryString);

    /**
     * 添加菜单
     *
     * @param menu
     * @return  返回sql执行之后影响的记录数
     */
    @Insert("INSERT INTO t_menu (name, linkUrl, path, priority, icon, description, parentMenuId, level) " +
            "VALUES (#{name}, #{linkUrl}, #{path}, #{priority}, #{icon}, #{description}, #{parentMenuId}, #{level})")
    int add(Menu menu);

    /**
     * 根据 id 查询菜单
     *
     * @param id 菜单id
     * @return
     */
    @Select("select * from t_menu where id = #{id}")
    Menu findById(Integer id);

    /**
     * 根据菜单id 查询子级菜单数量
     *
     * @param id
     * @return 返回子级菜单数量
     */
    @Select("select count(*) from t_menu where parentMenuId = #{id}")
    int findByParentMenuId(Integer id);

    /**
     * 根据菜单id 删除菜单
     *
     * @param id
     * @return
     */
    @Delete("DELETE from t_menu WHERE id = #{id}")
    int delete(Integer id);

    /**
     * 编辑菜单
     *
     * @param menu
     * @return 返回sql执行之后影响的记录数
     */
    @Update("UPDATE t_menu set name = #{name} , linkUrl = #{linkUrl} " +
            ", path = #{path} , priority = #{priority} " +
            ", icon = #{icon} , description = #{description} " +
            ", parentMenuId = #{parentMenuId} , level = #{level} WHERE id = #{id}")
    int edit(Menu menu);

    /**
     * 查询所有菜单
     * @return
     */
    @Select("select * from t_menu")
    List<Menu> findAll();


    //根据登录名查询菜单
    List<Menu> findMenuByUserName(String username);
}
